package com.cci.kangdao.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.cci.kangdao.service.ServiceAgreementService;
import com.cci.kangdao.utilTool.ParameterTool;

/**
 * 
 * 主动服务协议
 * @author Eric Mu
 *
 */
@Controller
public class ServiceAgreementController {

	private Logger log = Logger.getLogger(this.getClass().getName());
	
	@Autowired
	private ServiceAgreementService serviceAgreementService;
	
	/**
	 * 获取主动服务手机号
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getServicePhone.aspx")
	public ModelAndView getServicePhone(HttpServletRequest request, HttpServletResponse response) throws Exception {

		try{
			return serviceAgreementService.getServicePhone(response);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			ParameterTool.writeErrorResponse(response, -99);
		}
		return null;
	}
	
	/**
	 * 获取主动服务签署信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getServiceAggrementDetail.aspx")
	public ModelAndView getServiceAggrementDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {

		try{
			return serviceAgreementService.getServiceAggrementDetail(request, response);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			ParameterTool.writeErrorResponse(response, -99);
		}
		return null;
	}
	
	/**
	 * 签署主动服务协议
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/signServiceAgreement.aspx")
	public ModelAndView signServiceAgreement(HttpServletRequest request, HttpServletResponse response) throws Exception {

		try{
			return serviceAgreementService.signServiceAgreement(request, response);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			ParameterTool.writeErrorResponse(response, -99);
		}
		return null;
	}
	
	/**
	 * 解除主动服务协议
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/terminateServiceAgreement.aspx")
	public ModelAndView terminateServiceAgreement(HttpServletRequest request, HttpServletResponse response) throws Exception {

		try{
			
			String recordId = request.getParameter("recordId");
			
			return serviceAgreementService.terminateServiceAgreement(response, recordId);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			ParameterTool.writeErrorResponse(response, -99);
		}
		return null;
	}


	/**
	 * 解除主动服务协议
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/resynchronize.aspx")
	public ModelAndView resynchronize(HttpServletRequest request, HttpServletResponse response) throws Exception {

		try{

			String param = request.getParameter("param");

			return serviceAgreementService.resynchronize(response, param);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);

			ParameterTool.writeErrorResponse(response, -99);
		}
		return null;
	}



	@RequestMapping(value = "/getLicensePlateNumberByEsn.aspx", method= RequestMethod.POST)
	public ModelAndView getLicensePlateNumberByEsn(@RequestBody String paramStr, HttpServletRequest request,
												   HttpServletResponse response ) {
		try{
			log.info("接口传参为"+paramStr);
			return serviceAgreementService.getLicensePlateNumberByEsn(paramStr,request,response);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			String msg = "{\"status\": 1,\"msg\": \"系统异常\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}
}
